package com.slofzx.nuoda.evaluation.project.builder;

import com.slofzx.nuoda.common.util.DataConvertUtil;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import com.slofzx.nuoda.evaluation.project.vo.BatchCommonDataVO;
import com.slofzx.nuoda.evaluation.project.vo.ProjectInfoVO;
import com.slofzx.nuoda.evaluation.project.vo.ProjectReportDetailYearVO;
import com.slofzx.nuoda.evaluation.steretype.ExcelExportItem;
import com.slofzx.nuoda.excel.entity.SheetConfig;
import com.slofzx.nuoda.excel.entity.TemplateConfig;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class ResultExportExcelBuilder extends AbstractExcelExtendBuilder {

    private List<Map<String,Object>> projectMapList;

    private Map<String, List<ProjectReportDetailYearVO>> projectReportDetailYearVOMap;
    private  List<BatchCommonDataVO> batchCommonYearVOList;
    protected Map<String, List<ExcelExportItem>> excelExportItemGroupMap = new HashMap<>();

    public ResultExportExcelBuilder(TemplateConfig config, Map<String, List<ExcelExportItem>> excelExportItemGroupMap) {
        super(config);
        this.excelExportItemGroupMap = excelExportItemGroupMap;
    }

    public ResultExportExcelBuilder projectMapList(List<Map<String,Object>> projectMapList) {
        this.projectMapList = projectMapList;
        return this;
    }

    public ResultExportExcelBuilder projectReportDetailYearVOMap(Map<String, List<ProjectReportDetailYearVO>> projectReportDetailYearVOMap) {
        this.projectReportDetailYearVOMap = projectReportDetailYearVOMap;
        return this;
    }

    public ResultExportExcelBuilder batchCommonDataVOList(List<BatchCommonDataVO> batchCommonYearVOList) {
        this.batchCommonYearVOList = batchCommonYearVOList;
        return this;
    }

    @Override
    public AbstractExcelExtendBuilder yearList(List<Integer> yearList) {
        return super.yearList(yearList);
    }



    @Override
    public List<Map<String, Object>> getDataList(SheetConfig sheetConfig) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 0; i < projectMapList.size(); i++) {
            final Map<String,Object> projectInfoMap = projectMapList.get(i);
            final int index = i + 1;
            final HashMap<String, Object> dataMap = new HashMap<String, Object>() {{
                put("order", index);
            }};
            dataList.add(dataMap);
            if (projectInfoMap == null) {
                continue;
            }
            // 把数据提取到map中
            dataMap.putAll(projectInfoMap);
            List<ProjectReportDetailYearVO> yearDataList = projectReportDetailYearVOMap.get(projectInfoMap.get("id"));

            // 将绝对年份转换为相对年份
            final int projectYear = DataConvertUtil.parse2Int(projectInfoMap.get("project_year"));
            for (int j = yearDataList.size()-1; j >=0 ; j--) {
                ProjectReportDetailYearVO projectReportDetailYearVO = yearDataList.get(j);
                final Integer year = projectReportDetailYearVO.getYear();
                if (year < projectYear) {
                    yearDataList.remove(j);
                    continue;
                }
                projectReportDetailYearVO.setYear(year - projectYear+ 1);
            }
//            for (int j = batchCommonYearVOList.size()-1; j >=0 ; j--) {
//                BatchCommonDataVO batchCommonDataVO = batchCommonYearVOList.get(j);
//                final Integer year = batchCommonDataVO.getYear();
//                if (year < projectYear) {
//                    batchCommonYearVOList.remove(j);
//                    continue;
//                }
//                batchCommonDataVO.setYear(year - projectYear+ 1);
//            }
            yearDataList.sort(Comparator.comparing(ProjectReportDetailYearVO::getYear));
            batchCommonYearVOList.sort(Comparator.comparing(BatchCommonDataVO::getYear));
            Map<String,Object> yearDataMap = BusiUtil.parseYearDataList2Map(yearDataList,excelExportItemGroupMap,ProjectReportDetailYearVO.class);
            Map<String,Object> batchCommonDataMap = BusiUtil.parseYearDataList2Map(batchCommonYearVOList,excelExportItemGroupMap,BatchCommonDataVO.class);
            if(yearDataMap!=null) {
                dataMap.putAll(yearDataMap);
            }
            if (batchCommonDataMap != null) {
                dataMap.putAll(batchCommonDataMap);
            }
            // 计算公式
            computeExpression(sheetConfig,dataMap);
        }
        for (Map<String, Object> stringObjectMap : dataList) {
            for (String key : stringObjectMap.keySet()) {
                final Object value = stringObjectMap.get(key);
               stringObjectMap.put(key,value == null?StringUtils.EMPTY:String.valueOf(value));
            }
        }

        return dataList;
    }
}
